package com.sec.android.app.myfiles.module.download.providerapps;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import com.sec.android.app.myfiles.module.abstraction.AbsProviderImp;
import com.sec.android.app.myfiles.module.download.DownloadFileRecord.DownloadType;
import com.sec.android.app.myfiles.provider.DbTableInfo;
import com.sec.android.app.myfiles.provider.DbTableInfo.COLUMN_ID;
import com.sec.android.app.myfiles.util.chain.Chain;

/**
 * Abstraction class of Download app's Provider.
 *
 * @author jae.bae
 */
public abstract class AbsSupportAppProviderImp implements Chain<AbsSupportAppProviderImp> {
    protected Context mContext;
    protected SQLiteDatabase mDB;
    protected ContentResolver mContentResolver;
    protected AbsSupportAppProviderImp mNext;
    protected DbTableInfo mTableInfo;
    private AbsProviderImp mProvider;

    protected String COL_DownloadId;
    protected String COL_Name;
    protected String COL_FileType;
    protected String COL_Size;
    protected String COL_Date;
    protected String COL_Path;
    protected String COL_Ext;
    protected String COL_MimeType;
    protected String COL_Source;
    protected String COL_Description;
    protected String COL_Status;
    protected String COL_DownloadBy;
    protected String COL_FromSBrowser;

    public AbsSupportAppProviderImp(Context context, AbsProviderImp provider, SQLiteDatabase db) {
        mContext = context;
        mProvider = provider;
        mDB = db;
        mContentResolver = mContext.getContentResolver();
        mTableInfo = provider.getDbTableInfo();
        setColumnName();
    }

    protected void setColumnName() {
        if (mTableInfo != null) {
            COL_DownloadId = mTableInfo.getColumnName(COLUMN_ID.DOWNLOAD_ID);
            COL_Name = mTableInfo.getColumnName(COLUMN_ID.NAME);
            COL_FileType = mTableInfo.getColumnName(COLUMN_ID.FILE_TYPE);
            COL_Size = mTableInfo.getColumnName(COLUMN_ID.SIZE);
            COL_Date = mTableInfo.getColumnName(COLUMN_ID.DATE);
            COL_Path = mTableInfo.getColumnName(COLUMN_ID.PATH);
            COL_Ext = mTableInfo.getColumnName(COLUMN_ID.EXT);
            COL_MimeType = mTableInfo.getColumnName(COLUMN_ID.MIME_TYPE);
            COL_Source = mTableInfo.getColumnName(COLUMN_ID.SOURCE);
            COL_Description = mTableInfo.getColumnName(COLUMN_ID.DESCRIPTION);
            COL_Status = mTableInfo.getColumnName(COLUMN_ID.STATUS);
            COL_DownloadBy = mTableInfo.getColumnName(COLUMN_ID.DOWNLOAD_BY);
            COL_FromSBrowser = mTableInfo.getColumnName(COLUMN_ID.FROM_S_BROWSER);
        }
    }

    @Override
    public void setNext(AbsSupportAppProviderImp next) {
        mNext = next;
    }

    public void refresh(Uri uri) {
        _refresh(uri);

        if (mNext != null) {
            mNext.refresh(uri);
        }
    }

    protected int getProviderMaxIdEntry() {
        DownloadType downloadType = getDownloadType();

        int nRet = -1;

        String columns[] = {
                "MAX (" + mTableInfo.getColumnName(COLUMN_ID.DOWNLOAD_ID) + " )"};

        String selection = mTableInfo.getColumnName(COLUMN_ID.DOWNLOAD_BY) + " = " + downloadType.ordinal();
        try (Cursor c = mDB.query(mTableInfo.getTableName(), columns, selection, null, null, null, null)) {
            if (c.moveToFirst()) {
                nRet = c.getInt(0);
            }
        }
        return nRet;
    }

    protected void _refresh(Uri uri) {
        try (Cursor c = getCursor()) {
            if (c != null) {
                int nCnt = c.getCount();
                if (nCnt > 0) {
                    setColumnIndex(c);

                    ContentValues[] value = new ContentValues[nCnt];
                    c.moveToFirst();
                    int i = 0;

                    do {
                        ContentValues contentValue = getContentValues(c);
                        if (contentValue != null) {
                            value[i++] = contentValue;
                        }
                    } while (c.moveToNext());

                    mProvider.bulkInsert(uri, value);
                }
            }
        }
    }

    public void clearUnnecessaryData() {
        if (needClearUnnecessaryData()) {
            _clearUnnecessaryData();
        }

        if (mNext != null) {
            mNext.clearUnnecessaryData();
        }
    }

    private void _clearUnnecessaryData() {
        DownloadType downloadType = getDownloadType();
        int maxId = getProviderMaxIdEntry();
        StringBuilder selection = new StringBuilder();
        selection.append(mTableInfo.getColumnName(COLUMN_ID.DOWNLOAD_BY))
                .append('=')
                .append(downloadType.ordinal())
                .append(" AND ")
                .append(mTableInfo.getColumnName(COLUMN_ID.DOWNLOAD_ID))
                .append('<')
                .append(maxId)
                .append(" AND ")
                .append(mTableInfo.getColumnName(COLUMN_ID.DOWNLOADED_ITEM_VISIBILITY))
                .append("=0");
        mDB.delete(mTableInfo.getTableName(), selection.toString(), null);
    }


    protected abstract Cursor getCursor();

    protected abstract ContentValues getContentValues(Cursor cursor);

    protected abstract void setColumnIndex(Cursor cursor);

    protected abstract boolean needClearUnnecessaryData();

    protected abstract DownloadType getDownloadType();
}
